﻿//LookUp Manager ver.2.0
//Fully DBPUX instruction set compliant.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UnityScriptEngine;

namespace MBBHotelReservation
{
    public partial class frmLookUpManager : Form
    {
        UnityProcessor proc = new UnityProcessor();
        Threader t = new Threader();
        UnityScript[] data = new UnityScript[0];

        bool isAdding = true;
        int curDat = -1;
        public frmLookUpManager()
        {
            InitializeComponent();
            InitializeFrameSliderForm();
        }
        private void InitializeForm()
        {
            label4.Text = "Tips :\r\n" +
                "- You can select a key on the drop down list provided.\r\n" +
                "- You can add new keys by typing the new key in the drop down list text field.\r\n" +
                "- Add new data to key at the Add/Edit Box.\r\n" +
                "- Edit a data by double clicking the data and make modification at the Add/Edit Box.\r\n" +
                "- Click the Confirm button to apply the changes.";
            DoEvents();
            UnityEvents();
            LoadKeys();
        }
        void DoEvents()
        {
            txtKey.SelectedIndexChanged += new EventHandler(delegate
            {
                States(false);
                UnityScript u = new UnityScript("GETDATA", 1);
                u.Script_Add(Script.DBPUX_BEGIN);
                u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fData AS 'Data Value', fStamp FROM tbllookup WHERE fKey='{0}' ORDER BY fData ASC",
                    txtKey.Text)
                    );
                u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fKey, fData, fStamp FROM tbllookup WHERE fKey='{0}' ORDER BY fData ASC",
                    txtKey.Text)
                    );
                u.Script_Add(Script.DBPUX_COMMIT);
                proc.Run(u);
            });
            btnCancel.Click += new EventHandler(delegate
            {
                States(true);
                btnCancel.Enabled = false;
                txtData.Text = "";
                isAdding = true;
            });
            btnDelete.Click += new EventHandler(delegate
            {
                if (grdList.SelectedRows.Count > 0)
                {
                    DialogResult dlg = MessageBox.Show("The selected data will be permanently deleted. Do you wish to continue?",
                                "LookUp Manager", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                    if (dlg == System.Windows.Forms.DialogResult.No) return;
                    States(false);
                    curDat = grdList.SelectedRows[0].Index;

                    UnityElement ue;
                    data[1].Data_Get(curDat, out ue);
                    UnityScript u = new UnityScript("DELETE", 1);
                    u.Script_Add(Script.DBPUX_BEGIN);
                    u.Script_Add(Script.DBPUX_DELETE,"tbllookup",
                        string.Format("(fID='{0}') AND (fStamp='{1}')", ue.Data_ID, ue.Data_Stamp));
                    u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fData AS 'Data Value', fStamp FROM tbllookup WHERE fKey='{0}' ORDER BY fData ASC",
                        txtKey.Text)
                        );
                    u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fKey, fData, fStamp FROM tbllookup WHERE fKey='{0}' ORDER BY fData ASC",
                        txtKey.Text)
                        );
                    u.Script_Add(Script.DBPUX_COMMIT);
                    proc.Run(u);
                }
            });
            grdList.DoubleClick += new EventHandler(delegate
            {
                if (grdList.SelectedRows.Count > 0)
                {
                    isAdding = false;
                    btnCancel.Enabled = true;
                    curDat = grdList.SelectedRows[0].Index;
                    txtData.Text = grdList.SelectedRows[0].Cells[0].Value.ToString();
                    txtData.Focus();
                    txtData.SelectionStart = txtData.Text.Length;
                    grpLookup.Enabled = false;
                }
            });
            btnCommit.Click += new EventHandler(delegate
            {
                bool isValid = true;
                if (txtKey.Text.Length == 0) isValid = false;
                if (txtData.Text.Length == 0) isValid = false;
                if (!isValid)
                {
                    MessageBox.Show("Invalid inputs detected!",
                   "LookUp Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                States(false);
                UnityScript u = new UnityScript("COMMIT", 1);
                u.Script_Add(Script.DBPUX_BEGIN);
                if (isAdding)
                {
                    u.Script_Add(Script.DBPUX_INSERT, "tbllookup(fKey, fData)",
                        new string[]{
                            txtKey.Text.ToUpper(),
                            txtData.Text
                        });
                }
                else
                {
                    UnityElement ue;
                    data[1].Data_Get(curDat, out ue);
                    u.Script_Add(Script.DBPUX_UPDATE, string.Format("tbllookup SET fKey='{0}', fData='{1}'",
                        txtKey.Text.ToUpper(),
                        txtData.Text),
                        string.Format("(fID='{0}') AND (fStamp='{1}')",ue.Data_ID, ue.Data_Stamp));
                }
                u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fData AS 'Data Value', fStamp FROM tbllookup WHERE fKey='{0}' ORDER BY fData ASC",
                    txtKey.Text)
                    );
                u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fKey, fData, fStamp FROM tbllookup WHERE fKey='{0}' ORDER BY fData ASC",
                    txtKey.Text)
                    );
                u.Script_Add(Script.DBPUX_COMMIT);
                txtData.Text = "";
                proc.Run(u);
            });
        }
        void UnityEvents()
        {
            proc.ProcessCompleted += new ProcessHandler(delegate(string id, string desc)
            {
                t.SafeCall(this, delegate
                {
                    switch (id)
                    {
                        case "GETKEYS":
                            UnityScript[] keys;
                            proc.DBPUX_Data_Fetch(out keys);
                            txtKey.Items.Clear();
                            txtKey.Items.AddRange(keys[0].ToList(0));
                            States(true);
                            break;
                        case "COMMIT":
                        case "DELETE":
                        case "GETDATA":
                            proc.DBPUX_Data_Fetch(out data);
                            grdList.DataSource = data[0].ToDataTable();
                            grdList.Columns[0].Width = (grdList.Width * 90)  / 100;
                            States(true);
                            break;
                    }
                });
            });
            proc.ProcessError += new ProcessHandler(delegate(string id, string desc)
            {
                t.SafeCall(this, delegate
                {
                    MessageBox.Show(desc, "LookUp Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    States(true);
                });
            });
        }

        void States(bool state)
        {
            grpAddEditBox.Enabled = state;
            grpLookup.Enabled = state;
            pnlWait.Visible = !state;
        }
        void LoadKeys()
        {
            States(false);
            UnityScript u = new UnityScript("GETKEYS", 1);
            u.Script_Add(Script.DBPUX_BEGIN);
            u.Script_Add(Script.DBPUX_QUERY, "SELECT DISTINCT fKey FROM tbllookup ORDER BY fKey ASC");
            u.Script_Add(Script.DBPUX_COMMIT);
            proc.Run(u);
        }
    }
}
